package classifier;

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;
/**
 * ClassifierController contains the RadioButtons and TextField and responds on user input
 */
public class ClassifierController extends JPanel implements ActionListener {
	ArrayList<JRadioButton> buttons = new ArrayList<JRadioButton>();
	
	private JRadioButton  TurboYes = new JRadioButton("Yes");
	private JRadioButton  TurboNo = new JRadioButton("No");
	private JRadioButton  EnginePowerYes = new JRadioButton("Yes");
	private JRadioButton  EnginePowerNo = new JRadioButton("No");
	private JRadioButton  SportBumperYes = new JRadioButton("Yes");
	private JRadioButton  SportBumperNo = new JRadioButton("No");
	private JRadioButton  SportRingYes = new JRadioButton("Yes");
	private JRadioButton  SportRingNo = new JRadioButton("No");
	private JRadioButton  CruiseControlYes = new JRadioButton("Yes");
	private JRadioButton  CruiseControlNo = new JRadioButton("No");
	private JRadioButton  ABSYes = new JRadioButton("Yes");
	private JRadioButton  ABSNo = new JRadioButton("No");
	private JRadioButton  ACYes = new JRadioButton("Yes");
	private JRadioButton  ACNo = new JRadioButton("No");
	private JRadioButton  MetalicYes = new JRadioButton("Yes");
	private JRadioButton  MetalicNo = new JRadioButton("No");

	private JLabel TurboLabel = new JLabel("Turbo");
	private JLabel EnginePowerLabel = new JLabel("EnginePower");
	private JLabel SportBumperLabel = new JLabel("SportBumper");
	private JLabel SportRingLabel = new JLabel("SportRing");
	private JLabel CruiseControlLabel = new JLabel("CruiseControl");
	private JLabel ABSLabel = new JLabel("ABS");
	private JLabel ACLabel = new JLabel("AC");
	private JLabel MetalicLabel = new JLabel("Metalic");
	
	private JLabel treeLabel = new JLabel();
	
	private JPanel TurboPanel = new JPanel(new GridLayout(1,2));
	private JPanel EnginePowerPanel = new JPanel(new GridLayout(1,2));
	private JPanel SportBumperPanel = new JPanel(new GridLayout(1,2));
	private JPanel SportRingPanel = new JPanel(new GridLayout(1,2));
	private JPanel CruiseControlPanel = new JPanel(new GridLayout(1,2));
	private JPanel ABSPanel = new JPanel(new GridLayout(1,2));
	private JPanel ACPanel = new JPanel(new GridLayout(1,2));
	private JPanel MetalicPanel = new JPanel(new GridLayout(1,2));
	
	private JPanel calcInsurancePanel = new JPanel(new BorderLayout());
	private JPanel displayTree = new JPanel(new FlowLayout(FlowLayout.LEFT));
	private JButton getCategoryButton = new JButton("Send");
	
	private ButtonGroup TurboButtonGroup = new ButtonGroup();
	private ButtonGroup EnginePowerButtonGroup = new ButtonGroup();
	private ButtonGroup SportBumperButtonGroup = new ButtonGroup();
	private ButtonGroup SportRingButtonGroup = new ButtonGroup();
	private ButtonGroup CruiseControlButtonGroup = new ButtonGroup();
	private ButtonGroup ABSButtonGroup = new ButtonGroup();
	private ButtonGroup ACButtonGroup = new ButtonGroup();
	private ButtonGroup MetalicButtonGroup = new ButtonGroup();
	
	private DecisionTree tree;
	private String[] featureNames;
	private String[] featureValues;
	private String category;
	private Feature[] features;
	private FeatureType ft;
	private String itemName = "";
	private int countSelected = 0;
	private JTextArea displayArea = new JTextArea();
	private JPanel questionPanel = new JPanel(new GridLayout(9,1));
	/**
	 * @param DecisionTree
	 * @param featureNames and FeatureType
	 */
	public ClassifierController(DecisionTree tree, String[] featureNames, FeatureType ft){
		this.tree = tree;
		this.featureNames = featureNames;
		this.ft = ft;
		
		features = new Feature[featureNames.length];
		featureValues = new String[featureNames.length];
		setLayout(new BorderLayout());
		
		JTabbedPane tabpane = new JTabbedPane();
		
		tabpane.add("Calc Insurance", createQuestionPanel());
		//gets the root node
		Node treeNode = tree.getRoot();
		tabpane.add("Display tree", createTree(treeNode));
		
		add(tabpane);
	}
	
	boolean leaf = false;
	//creates the tree to display on the GUI.
	//see the getTreePanel() function in Node class
	//this doesn't work correctly.. only displays the features not as a tree
	public JPanel createTree(Node treeNode){
		
		JPanel treePanel = new JPanel();
		treePanel.setPreferredSize(new Dimension(1600,1100));
		treePanel.setLayout(null);
		JScrollPane scrollFrame = new JScrollPane(treePanel);
		treePanel.setAutoscrolls(true);
		scrollFrame.setPreferredSize(new Dimension( 1500,1000));
		this.add(scrollFrame);
		//treePanel.setLayout(null);
		treeNode.getPieces(treePanel, 0, 750, 0);
		//treePanel.setVisible(true);
		
			//creates the function
			displayTree.add(treePanel);
			
		return displayTree;
	}
	
	public JLabel createTree(String string){
		//creates the function
		JLabel test = new JLabel(string);
		return test;
}
	/**
	 * Creates the question panel(questions + textfield)
	 * @return The panel
	 */
	public JPanel createQuestionPanel(){

		displayArea.setPreferredSize(new Dimension(50,100));
		
		TurboPanel.add(TurboLabel);
		TurboPanel.add(TurboYes);
		TurboPanel.add(TurboNo);
		
		EnginePowerPanel.add(EnginePowerLabel);
		EnginePowerPanel.add(EnginePowerYes);
		EnginePowerPanel.add(EnginePowerNo);
		
		SportBumperPanel.add(SportBumperLabel);
		SportBumperPanel.add(SportBumperYes);
		SportBumperPanel.add(SportBumperNo);
		
		SportRingPanel.add(SportRingLabel);
		SportRingPanel.add(SportRingYes);
		SportRingPanel.add(SportRingNo);
		
		CruiseControlPanel.add(CruiseControlLabel);
		CruiseControlPanel.add(CruiseControlYes);
		CruiseControlPanel.add(CruiseControlNo);
		
		ABSPanel.add(ABSLabel);
		ABSPanel.add(ABSYes);
		ABSPanel.add(ABSNo);
		
		ACPanel.add(ACLabel);
		ACPanel.add(ACYes);
		ACPanel.add(ACNo);
		
		MetalicPanel.add(MetalicLabel);
		MetalicPanel.add(MetalicYes);
		MetalicPanel.add(MetalicNo);
		
		questionPanel.add(TurboPanel);
		questionPanel.add(EnginePowerPanel);
		questionPanel.add(SportBumperPanel);
		questionPanel.add(SportRingPanel);
		questionPanel.add(CruiseControlPanel);
		questionPanel.add(ABSPanel);
		questionPanel.add(ACPanel);
		questionPanel.add(MetalicPanel);
		questionPanel.add(getCategoryButton);
		
		calcInsurancePanel.add(questionPanel, BorderLayout.CENTER);
		calcInsurancePanel.add(displayArea, BorderLayout.SOUTH);
		
		getCategoryButton.addActionListener(this);
		
		
		buttons.add(TurboYes);
		buttons.add(TurboNo);
		buttons.add(EnginePowerYes);
		buttons.add(EnginePowerNo);
		buttons.add(SportBumperYes);
		buttons.add(SportBumperNo);
		buttons.add(SportRingYes);
		buttons.add(SportRingNo);
		buttons.add(CruiseControlYes);
		buttons.add(CruiseControlNo);
		buttons.add(ABSYes);
		buttons.add(ABSNo);
		buttons.add(ACYes);
		buttons.add(ACNo);
		buttons.add(MetalicYes);
		buttons.add(MetalicNo);
		
		TurboYes.setSelected(true);
		EnginePowerYes.setSelected(true);
		SportBumperYes.setSelected(true);
		SportRingYes.setSelected(true);
		CruiseControlYes.setSelected(true);
		ABSYes.setSelected(true);
		ACYes.setSelected(true);
		MetalicYes.setSelected(true);
		
		TurboButtonGroup.add(TurboYes);
		TurboButtonGroup.add(TurboNo);
		
		EnginePowerButtonGroup.add(EnginePowerYes);
		EnginePowerButtonGroup.add(EnginePowerNo);
		
		SportBumperButtonGroup.add(SportBumperYes);
		SportBumperButtonGroup.add(SportBumperNo);
		
		SportRingButtonGroup.add(SportRingYes);
		SportRingButtonGroup.add(SportRingNo);
		
		CruiseControlButtonGroup.add(CruiseControlYes);
		CruiseControlButtonGroup.add(CruiseControlNo);
		
		ABSButtonGroup.add(ABSYes);
		ABSButtonGroup.add(ABSNo);
		
		ACButtonGroup.add(ACYes);
		ACButtonGroup.add(ACNo);
		
		MetalicButtonGroup.add(MetalicYes);
		MetalicButtonGroup.add(MetalicNo);
		
		return calcInsurancePanel;
	}
	@Override
	public void actionPerformed(ActionEvent e) {

		//if button is submitted
		if(e.getSource() == getCategoryButton){
			int countSelected = 0;
			//loops through array of button to see which ones are selected.
			for(int i = 0; i < buttons.size(); i++){
				if(buttons.get(i).isSelected()){
					if(buttons.get(i).getText() == "Yes"){
						featureValues[countSelected] = "1";
					}
					if(buttons.get(i).getText() == "No"){
						featureValues[countSelected] = "0";
					}
					countSelected++;
				}
			}
			//create features and assigns category based on the features
			if(countSelected >= 8){
				for(int i = 0; i < features.length; i++){
					features[i] = new Feature(featureNames[i], featureValues[i], ft);
				}
				category = tree.assignCategory(new Item("Car", features));
				displayArea.setText("Insurance status: " + category + "\n" + displayArea.getText());
			}else{
				displayArea.setText("Error: Please select all 8 options \n" + displayArea.getText());
			}
		}
		
	}

}
